#include <iostream>
#include <conio.h>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <list>
#include <algorithm>
#include <string.h>
#include <time.h>

#include "mnheap.h"
#include "test.h"
#include "uvector.h"
#include "sort_algorithm.h"
#include "DirectMemClass.h"

using namespace std;

//=============================================
//uvector test
static int out(int a)
{
    cout << a << " ";
    return 0;
}
static void vector_test(void)
{
    UVector<int> tvector;
    vector<int> svector;

    cout << "initilize!" << endl;
    cout << "tvector default size " << tvector.size() << " capaticy " << tvector.capacity() << endl;
    cout << "svector default size " << svector.size() << " capaticy " << svector.capacity() << endl;

    int i = 0;
    for(; i < 18; i++) tvector.push_back(i);
    for(i = 0; i < 18; i++) svector.push_back(i);

    cout << "assignment!" << endl;
    cout << "tvector default size " << tvector.size() << " capaticy " << tvector.capacity() << endl;
    cout << "svector default size " << svector.size() << " capaticy " << svector.capacity() << endl;

    for(UVector<int>::iterator iter = tvector.begin(); iter != tvector.end(); iter++)
        cout << *iter << " ";
    cout << endl;

    for(vector<int>::iterator iter = svector.begin(); iter != svector.end(); iter++)
        cout << *iter << " ";
    cout << endl;

    tvector[2] = 5;
    unique(tvector.begin(), tvector.end());
    cout << "unique" << endl;
    for_each(tvector.begin(), tvector.end(), out);
    cout << endl;
    cout << "tvector sort" << endl;
    sort(tvector.begin(), tvector.end());
    unique(tvector.begin(), tvector.end());
    cout << "for_each" << endl;
    for_each(tvector.begin(), tvector.end(), out);
    cout << endl;

    svector[2] = 5;
    unique(svector.begin(), svector.end());
    cout << "unique" << endl;
    for_each(svector.begin(), svector.end(), out);
    cout << endl;
    cout << "svector sort" << endl;
    sort(svector.begin(), svector.end());
    svector.erase(unique(svector.begin(), svector.end()), svector.end());

    cout << "for_each" << endl;
    for_each(svector.begin(), svector.end(), out);
    cout << endl;

}

void sort_test(void)
{
    int buf[100], i = 0;
    int t1= 0, t2=1;
    swap(t1, t2);

    for(i = 0; i < 100; i++)
    {
        buf[i] = i;
    }

    cout << "bubble sort" << endl;
    BubbleSort(buf, 0, 100);
    for(i = 0; i < 100; i++)
    {
        cout << buf[i] << " ";
    }
    cout << endl;
    cout << "select sort" << endl;
    SimpleSelectSort(buf, 0, 100);
    for(i = 0; i < 100; i++)
    {
        cout << buf[i] << " ";
    }
    cout << endl;
    cout << "insert sort" << endl;
    DirectInsertSort(buf, 0, 100);
    for(i = 0; i < 100; i++)
    {
        cout << buf[i] << " ";
    }
    cout << endl;
}

static void stl_test(void)
{

}

static char *upsort_str(const char *str)
{
    if(NULL == str) return NULL;

    char sarray[256] = {0};

    char *pStr = const_cast<char *>(str);
    char *pTmpStr = pStr;

    while(*pStr)
    {
        unsigned tmp = *pStr;

        sarray[tmp]++;
        pStr++;
    }

    pStr = new char[pStr - pTmpStr + 1];
    if(!pStr) return NULL;

    pTmpStr = pStr;
    for(int i = 0; i < 256; i++)
    {
        for(char t = sarray[i]; t > 0; t--)
        {
            *pStr++ = t;    //saaray[s]. dead two
        }
    }
    *pStr = 0;

    return pTmpStr;
}

static void printf_tips(const char *pstr)
{
    cout << endl;
    cout << "*******************************************" << endl;
    cout << pstr << endl;
    cout << endl;
}

int main()
{
    const char *pompt = "222^^^djdhgljd haha ok!";
    char *pstr = NULL;
    cout << "Hello World!" << endl;

    cout << "face test!" << endl;
    cout << "upsortstr!!!" << endl;
    cout << "string:" << pompt << endl;
    pstr = upsort_str(pompt);
    cout << "upsortstr:" << pstr << endl;
    delete [] pstr;
    pstr = NULL;

    printf_tips("STL_test");
    stl_test();

    printf_tips("stack_test");
    stack_test();

    printf_tips("overqueue_test");
    overqueue_test();

    printf_tips("array_test");
    array_test();

    printf_tips("vector_test");
    vector_test();

    printf_tips("ustring_test");
    ustring_test();

    printf_tips("stack_queue_test");
    stack_queue_test();

    printf_tips("slist_test");
    slist_test();

    printf_tips("sort test");
    sort_test();

    printf_tips("mn_heap test");
    test_heap();

    printf_tips("Direct Memory Test");
    DirectMemClassTest();


    printf_tips("Other Test");

    factory_test();
    singleton_test();
    SingleFactory_test();
    observe_test();

    srand(unsigned (time(0)));

#define TT (rand())

    cout << "rand " <<  rand() << endl;
    cout << TT << endl;

    _getch();
    return 0;
}

